#pragma once

#include <vector>
#include <CUtil.h>

template <class T>
class Container
{
 public:
  vector<T>  objects;
  int no_objects;
 public:
   
  double count;

  T operator[]( int j) {
    if (j<objects.size())
      return objects[j];
    //    else
    //     throw Lex("Container: trying to access index " +Util::toString((double)j) +" of indices " + Util::toString(objects.size()));
  }
  
  T Get(int j) {
    if (j<objects.size())
      return objects[j];
    else
      throw string("Container: trying to access index " +CUtil::toString(j) +" of indices " + CUtil::toString(objects.size()));
  }

  void SetCount(int cnt) {
    count = cnt;
    Maintain();
  }

  int GetCount() {
    return count;
  }

  void Maintain() {

    if (objects.size() == count) 
      return;

    if (count>objects.size()) { // add objects
      for (int i=0;i<count - objects.size();i++)  {
	objects.push_back(InitializeObject());
      }
    }
    if (count<objects.size()) {
      for (int i=0;i<objects.size() - count; i++)
	objects.erase(objects.begin() + rand()%objects.size());
    }
    
  }
  virtual T InitializeObject() = 0;
  


};
